# -*-coding: Utf-8 -*-
# @File : models .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2024/5/7

from .db_handler import DBHandler


class Admin(DBHandler):
    def __init__(self, name, password, salt):
        super().__init__()
        self.name = name
        self.password = password
        self.salt = salt

    @classmethod
    def show_data_info(cls):
        flag, name_list = cls.show_name()
        if not flag:
            return flag, name_list
        print(f"**** 信息如下 ****")
        for index, name in enumerate(name_list, start=1):
            flag, obj = cls.select(name=name)
            index = str(index).rjust(5, '0')
            print(f"""
            当前管理员编  号 :>>>> {index} 
            当前管理员用户名 :>>>> {obj.name}
            """)
        print(f"**** 信息如上 ****")
        return True, "查看成功"

    def __str__(self):
        return self.name


class School(DBHandler):
    def __init__(self, name, addr, desc):
        self.name = name
        self.addr = addr
        self.desc = desc
        self.courses_list = []

    @classmethod
    def show_data_info(cls):
        flag, name_list = cls.show_name()
        if not flag:
            return flag, name_list
        print(f"**** 信息如下 ****")
        for index, name in enumerate(name_list, start=1):
            flag, obj = cls.select(name=name)
            index = str(index).rjust(5, '0')
            if hasattr(obj, 'courses_list'):
                course_open = ', '.join([course.name for course in obj.courses_list])
            else:
                course_open = None
            print(f"""
            当前学校编号 :>>>> {index} 
            当前学校名称 :>>>> {obj.name}
            当前学校地址 :>>>> {obj.addr}
            当前学校简介 :>>>> {obj.desc}
            当前学校开始课程 :>>>> {'当前学校无课程开设' if not course_open else course_open}
            """)
        print(f"**** 信息如上 ****")
        return True, "查看成功"

    def __str__(self):
        return self.name


class Course(DBHandler):
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period
        self.school = None
        self.teacher = None
        self.student_list = []

    @classmethod
    def show_data_info(cls):
        flag, name_list = cls.show_name()
        if not flag:
            return flag, name_list
        print(f"**** 信息如下 ****")
        for index, name in enumerate(name_list, start=1):
            flag, obj = cls.select(name=name)
            index = str(index).rjust(5, '0')
            print(f"""
            当前学校编号 :>>>> {index} 
            当前课程名称 :>>>> {obj.name}
            当前课程价格 :>>>> {obj.price}
            当前课程周期 :>>>> {obj.period}
            当前课程关联学校 :>>>> {'当前课程无学校开设' if not obj.school else obj.school.name}
            """)
        print(f"**** 信息如上 ****")
        return True, "查看成功"

    def __str__(self):
        return self.name


class Teacher(DBHandler):
    def __init__(self, name, password, salt):
        self.name = name
        self.password = password
        self.salt = salt
        self.courses_list = []

    @classmethod
    def show_data_info(cls):
        # 【一】获取所有的讲师名单
        flag, name_list = cls.show_name()
        if not flag:
            return False, name_list
        # 【二】查看所有讲师的信息
        print(f"**** 信息如下 ****")
        for index, name in enumerate(name_list, start=1):
            flag, obj = cls.select(name=name)
            index = str(index).rjust(5, '0')
            if hasattr(obj, 'courses_list'):
                course_teach = ', '.join([course.name for course in obj.courses_list])
            else:
                course_teach = None
            print(f"""
            当前讲师编号 :>>>> {index} 
            当前讲师账号 :>>>> {obj.name}
            当前讲师授课 :>>>> {'当前讲师无课程教授' if not course_teach else course_teach}
            """)
        print(f"**** 信息如上 ****")
        return True, "查看成功"

    def __str__(self):
        return self.name


class Student(DBHandler):
    def __init__(self, name, password, salt):
        self.name = name
        self.password = password
        self.salt = salt
        self.course = {}

    def show_data_info(self):
        if not self.course:
            return False, f'当前无课程信息，请先添加课程信息!'
        print(f"**** 信息如下 ****")
        for index, course_data in enumerate(self.course.items(), start=1):
            course_name, course_score = course_data
            index = str(index).rjust(5, '0')
            print(f"""
            当前课程编号 :>>>> {index} 
            当前课程名称 :>>>> {course_name}
            当前课程分数 :>>>> {course_score}
            """)
        print(f"**** 信息如上 ****")
        return True, "查看成功"
